//
//  DtChannelApi.m
//  kq_flutter_core_widget
//
//  Created by wtb on 2024/12/27.
//

#import "DtChannelApi.h"
#import <DTShareKit/DTOpenAPI.h>

static DtChannelApi *dtApi = nil;

@interface DtChannelApi()

@property(nonatomic,copy) NSString          *appId_record;
 
@end

@implementation DtChannelApi

+ (instancetype _Nullable)defualtDtChannelApi{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (dtApi == nil) {
            dtApi = [[DtChannelApi alloc] init];
        }
    });
    return dtApi;
}

-(void)handleDTShareTextCall:(FlutterMethodCall *)call result:(FlutterResult)result{
    BOOL isInitail = [DTOpenAPI isDingTalkInstalled];
    if (!isInitail) {
        result(@(-3));
        return;
    }
    if (call.arguments[@"appId"]) {
        NSString *appId = call.arguments[@"appId"];
        if (self.appId_record != nil && ![self.appId_record isEqualToString:@""]) {
            //存在
            BOOL isNoExit = ![appId isEqualToString:self.appId_record];
            if (isNoExit) {
                //传入的数值有变化
                //需要重新注册
                BOOL isSuccess = [DTOpenAPI registerApp:appId];
                if (!isSuccess) {
                    //失败 结束
                    return result(@{@"code":@"-1"});
                }
             }
        }else{
            //不存在
            BOOL isSuccess = [DTOpenAPI registerApp:appId];
            if (!isSuccess) {
                //失败 结束
                return result(@{@"code":@"-1"});
            }
        }
        self.appId_record = appId;
    }
    
    DTSendMessageToDingTalkReq *sendMessageReq = [[DTSendMessageToDingTalkReq alloc] init];
    
    DTMediaMessage *mediaMessage = [[DTMediaMessage alloc] init];
    //此处规定，分享只显示中文
    mediaMessage.title = call.arguments[@"title"];
    mediaMessage.messageDescription = call.arguments[@"msgContent"];
    
    DTMediaTextObject *obj = [[DTMediaTextObject alloc] init];
    obj.text = call.arguments[@"msgContent"];
    
    mediaMessage.mediaObject = obj;
    sendMessageReq.message = mediaMessage;

    BOOL res = [DTOpenAPI sendReq:sendMessageReq];
    if (res){
        result(@0);
    }else{
        result(@(-1));
    }
}

-(void)handleDTShareWebCall:(FlutterMethodCall *)call result:(FlutterResult)result{
    BOOL isInitail = [DTOpenAPI isDingTalkInstalled];
    if (!isInitail) {
        result(@(-3));
        return;
    }
    if (call.arguments[@"appId"]) {
        NSString *appId = call.arguments[@"appId"];
        if (self.appId_record != nil && ![self.appId_record isEqualToString:@""]) {
            //存在
            BOOL isNoExit = ![appId isEqualToString:self.appId_record];
            if (isNoExit) {
                //传入的数值有变化
                //需要重新注册
                BOOL isSuccess = [DTOpenAPI registerApp:appId];
                if (!isSuccess) {
                    //失败 结束
                    return result(@{@"code":@"-1"});
                }
             }
        }else{
            //不存在
            BOOL isSuccess = [DTOpenAPI registerApp:appId];
            if (!isSuccess) {
                //失败 结束
                return result(@{@"code":@"-1"});
            }
        }
        self.appId_record = appId;
    }
    
    if (call.arguments[@"url"]) {
        DTSendMessageToDingTalkReq *sendMessageReq = [[DTSendMessageToDingTalkReq alloc] init];

        DTMediaMessage *mediaMessage = [[DTMediaMessage alloc] init];
        mediaMessage.title = call.arguments[@"title"];
        mediaMessage.messageDescription = call.arguments[@"msgContent"];
    //    mediaMessage.thumbData = UIImagePNGRepresentation([UIImage imageNamed:@"ic_logo.png"]);
        if ([call.arguments[@"iconUrl"] isValid]) {
            mediaMessage.thumbURL = call.arguments[@"iconUrl"];
        }else{
            FlutterStandardTypedData *typeData = call.arguments[@"icon"];
            mediaMessage.thumbData = typeData.data;
        }
        DTMediaWebObject *webObject = [[DTMediaWebObject alloc] init];
        webObject.pageURL = call.arguments[@"url"];
        mediaMessage.mediaObject = webObject;
        sendMessageReq.message = mediaMessage;

        BOOL res = [DTOpenAPI sendReq:sendMessageReq];
        if (res){
            result(@(0));
        }else{
            result(@(-1));
        }
    }else{
        result(@(-1));
    }
}

-(void)handleDTShareImageCall:(FlutterMethodCall *)call result:(FlutterResult)result{
    BOOL isInitail = [DTOpenAPI isDingTalkInstalled];
    if (!isInitail) {
        result(@(-3));
        return;
    }
    if (call.arguments[@"appId"]) {
        NSString *appId = call.arguments[@"appId"];
        if (self.appId_record != nil && ![self.appId_record isEqualToString:@""]) {
            //存在
            BOOL isNoExit = ![appId isEqualToString:self.appId_record];
            if (isNoExit) {
                //传入的数值有变化
                //需要重新注册
                BOOL isSuccess = [DTOpenAPI registerApp:appId];
                if (!isSuccess) {
                    //失败 结束
                    return result(@{@"code":@"-1"});
                }
             }
        }else{
            //不存在
            BOOL isSuccess = [DTOpenAPI registerApp:appId];
            if (!isSuccess) {
                //失败 结束
                return result(@{@"code":@"-1"});
            }
        }
        self.appId_record = appId;
    }
    
    DTSendMessageToDingTalkReq *sendMessageReq = [[DTSendMessageToDingTalkReq alloc] init];

    DTMediaMessage *mediaMessage = [[DTMediaMessage alloc] init];
    mediaMessage.title = call.arguments[@"title"];
    
    DTMediaImageObject *webObject = [[DTMediaImageObject alloc] init];
    FlutterStandardTypedData *typeData = call.arguments[@"bytes"];
    if (typeData != nil) {
        webObject.imageData = typeData.data;
    } else if(call.arguments[@"url"]){
        NSString *url = call.arguments[@"url"];
        NSURL *imageUrl = [NSURL URLWithString:url];
        webObject.imageData = [NSData dataWithContentsOfURL:imageUrl];
    } else {
        NSString *localPath = call.arguments[@"localPath"];
        NSURL *imageUrl = [NSURL URLWithString:localPath];
        webObject.imageData = [NSData dataWithContentsOfFile:imageUrl.path];
    }
    mediaMessage.mediaObject = webObject;
    sendMessageReq.message = mediaMessage;

    BOOL res = [DTOpenAPI sendReq:sendMessageReq];
    if (res){
        result(@(0));
    }else{
        result(@(-1));
    }
}

@end
